/*
 * Copyright 2011 Kim Lindhardt Madsen
 *
 *    Licensed under the Apache License, Version 2.0 (the "License");
 *    you may not use this file except in compliance with the License.
 *    You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *    Unless required by applicable law or agreed to in writing, software
 *    distributed under the License is distributed on an "AS IS" BASIS,
 *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *    See the License for the specific language governing permissions and
 *    limitations under the License.
 */

package dk.lindhardt.gwt.geie.server.convert;

import dk.lindhardt.gwt.geie.shared.Cell;
import jxl.CellType;

import java.util.HashMap;
import java.util.Map;

/**
 * User: AnAmuser
 * Date: 12-08-11
 * <p/>
 * A register of what cell converter strategies that should be used. By default the register contains:
 * <ul>
 *    <li>{@link EmptyCellConverterStrategy}</li>
 *    <li>{@link LabelCellConverterStrategy}</li>
 *    <li>{@link BooleanCellConverterStrategy}</li>
 *    <li>{@link NumberCellConverterStrategy}</li>
 *    <li>{@link DateCellConverterStrategy}</li>
 *    <li>{@link NumberFormulaCellConverterStrategy}</li>
 *    <li>{@link BooleanFormulaCellConverterStrategy}</li>
 * </ul>
 * The register can be modified if needed. Only one strategy can exist pr. type. That means that if you register another
 * strategy for fx the Boolean cell type then this strategy will overwrite the existing strategy.
 */
public class CellConverterStrategyRegister {

   private static Map<CellType, CellConverterStrategy> jxlMap =
         new HashMap<CellType, CellConverterStrategy>();
   private static Map<Class<? extends Cell>, CellConverterStrategy> geieMap =
         new HashMap<Class<? extends Cell>, CellConverterStrategy>();

   static {
      registerStrategy(new EmptyCellConverterStrategy());
      registerStrategy(new LabelCellConverterStrategy());
      registerStrategy(new BooleanCellConverterStrategy());
      registerStrategy(new NumberCellConverterStrategy());
      registerStrategy(new DateCellConverterStrategy());
      registerStrategy(new NumberFormulaCellConverterStrategy());
      registerStrategy(new BooleanFormulaCellConverterStrategy());
   }

   /**
    * Register a cell converter strategy.
    * @param strategy a strategy
    */
   public static void registerStrategy(CellConverterStrategy strategy) {
      for (CellType cellType : strategy.getJXLTypes()) {
         jxlMap.put(cellType, strategy);
      }
      Class<? extends Cell> geieType = strategy.getGeieType();
      if (geieType != null) {
         geieMap.put(geieType, strategy);
      }
   }

   /**
    * Gets cell converter strategy for the given cell type
    * @param type the cell type
    * @return the strategy
    */
   public static CellConverterStrategy getStrategy(CellType type) {
      return jxlMap.get(type);
   }

   /**
    * Gets cell converter strategy for the given cell type
    * @param type the cell type
    * @return the strategy
    */
   public static CellConverterStrategy getStrategy(Class<? extends Cell> type) {
      return geieMap.get(type);
   }
}
